package com.color.admin.system.controller;


import com.color.admin.common.core.base.BaseController;
import com.color.admin.common.core.constant.Constant;
import com.color.admin.common.core.page.PageRequest;
import com.color.admin.common.core.result.ResultData;
import com.color.admin.common.logger.annotation.Log;
import com.color.admin.system.entity.BaseEntity;
import com.color.admin.system.entity.SysRole;
import com.color.admin.system.entity.query.RoleQueryCriteria;
import com.color.admin.system.servie.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/12/22
 */
@Tag(name = "role-controller", description = "角色管理接口")
@RequestMapping("/system/role")
@RestController
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Log("角色新增")
    @Operation(summary = "角色新增", description = "角色新增修改操作")
    @PostMapping
    public ResultData save(@Parameter(name = "角色新增") @RequestBody @Validated SysRole sysRole) {
        roleService.save(sysRole);
        return decide(Constant.INSERT);
    }

    @Log("角色修改")
    @Operation(summary = "角色修改", description = "角色修改操作")
    @PutMapping
    public ResultData update(@Parameter(name = "角色修改") @RequestBody @Validated(BaseEntity.update.class) SysRole sysRole) {
        roleService.update(sysRole);
        return decide(Constant.UPDATE);
    }

    @Log("角色删除")
    @Operation(summary = "角色删除", description = "批量删除角色")
    @DeleteMapping
    public ResultData delete(@Parameter(name = "数据ID数组") @RequestBody Set<String> ids) {
        roleService.delete(ids);
        return decide(Constant.DELETE);
    }

    @Log("所有角色")
    @Operation(summary = "所有角色", description = "查询所有角色.可选字段，可根据名称进行查询")
    @GetMapping("/roleAll")
    public ResultData roleAll(@Parameter(description = "角色名称模糊查询（非必传）")
                              @RequestBody(required = false) String roleName) {
        return decide(roleService.roleAll(roleName));
    }

    @Log("查询角色")
    @Operation(summary = "查询角色", description = "根据ID查询角色数据")
    @GetMapping
    public ResultData get(@Parameter(name = "角色数据ID") @RequestParam String roleId) {
        return decide(roleService.get(roleId));
    }

    @Log("角色模糊分页")
    @Operation(summary = "分页查询", description = "角色模糊分页查询")
    @PostMapping("/list")
    public ResultData list(@RequestBody PageRequest<RoleQueryCriteria> request) {
        return decide(roleService.list(request));
    }
}
